/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.common.page;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
@Data
@ApiModel(value = "分页数据")
public class PageData<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "总记录数")
    private long totalRows;
    @ApiModelProperty(value = "当前页")
    private long current = 1;
    @ApiModelProperty(value = "分页数")
    private long size = 20;
    @ApiModelProperty(value = "总页数")
    private long totalPage;
    @ApiModelProperty(value = "列表数据")
    private List<T> list;

    /**
     * 分页
     *
     * @param list      列表数据
     * @param totalRows 总记录数
     */
    public PageData(List<T> list, long totalRows) {
        this.list = list;
        this.totalRows = totalRows;
        this.totalPage = this.totalRows % size == 0 ? this.totalRows / size : this.totalRows / size + 1;
    }

    public PageData(List<T> list, long size, long current, long totalRows) {
        this.list = list;
        this.totalRows = totalRows;
        this.current = current;
        this.size = size;
        this.totalPage = this.totalRows % size == 0 ? this.totalRows / size : this.totalRows / size + 1;
    }
}